package SudokuViews;

import com.example.sv2.R;

import Sudoku.SudokuMode;
import Sudoku.SudokuResult;
import SudokuViews.GameViewV2.OnBtnBackClick;
import SudokuViews.GameViewV2.OnBtnStartClick;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.Align;
import android.util.Log;
import android.view.MotionEvent;
import Core.AGEngine;
import Core.AGView;
import Core.Controls.AGButton;
import Core.Controls.AGControl;
import Core.Controls.IAGEventHandler;

public class ResultView  extends AGView {

	private RectF _successRect;
	private SudokuResult _result;
	private int numIndex[] = { R.drawable.n1, 
			R.drawable.n2,
			R.drawable.n3,
			R.drawable.n4,
			R.drawable.n5,
			R.drawable.n6,
			R.drawable.n7,
			R.drawable.n8,
			R.drawable.n9};

	private AGButton _btnStart;
	private AGButton _btnBack;
	
	public ResultView(AGEngine engine, SudokuResult result) {
		super(engine);
		// TODO Auto-generated constructor stub
		this._result = result;
		// 227 106
		_successRect = new RectF(126,100,353,206);
		
		this._btnStart = new AGButton(engine, R.drawable.bntpause);
		this._btnStart.setPostion(180, 520);
		this._btnStart.addClickEvent(new OnBtnStartClick(this));
		this.controls.add(this._btnStart);
		
		this._btnBack = new AGButton(engine, R.drawable.btnreturn);
		this._btnBack.setPostion(240, 520);
		this._btnBack.addClickEvent(new OnBtnBackClick(this));
		this.controls.add(this._btnBack);
	}

	@Override
	public void onUpdate(AGEngine engine) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onRender(AGEngine engine) {
		// TODO Auto-generated method stub
		engine.graphics.drawBitmap(this._successRect, engine.resource.getImage(R.drawable.success), null);

		String gameTimeString = String.format("%s", this._result.gameTime);
		int timeNumCount= gameTimeString.length();
		float timeRectWidth = 75 + 50 + timeNumCount* 50;
		
		float lastRight = (480 - timeRectWidth) / 2;
		float top = this._successRect.bottom + 50;
		RectF timerRect = new RectF(lastRight,top, lastRight + 75, top + 50);
		engine.graphics.drawBitmap(timerRect,
				engine.resource.getImage(R.drawable.timer));
		lastRight = timerRect.right;
		for(int timeNumIndex=0; timeNumIndex< timeNumCount; timeNumIndex++){
			RectF numRect = new RectF(lastRight,top, lastRight + 50, top + 50);
			int timeNum = Integer.parseInt(String.format("%s", gameTimeString.charAt(timeNumIndex)));
			Log.d("sv2", String.format("%s %s %s", gameTimeString, timeNumCount, timeNum));
			int numIndex = timeNum - 1;
			if(numIndex < 0){
				numIndex = 0;
			}
			engine.graphics.drawBitmap(numRect,
					engine.resource.getImage(this.numIndex[numIndex]), null);
			lastRight = numRect.right;
		}
		RectF secondRect = new RectF(lastRight,top, lastRight + 50,top + 50);
		engine.graphics.drawBitmap(secondRect,
				engine.resource.getImage(R.drawable.seconde));
	}
	
	class OnBtnBackClick implements IAGEventHandler{
		private AGView view;
		public OnBtnBackClick(AGView view){
			this.view = view;
		}
		
		@Override
		public void onHandle(AGControl control) {
			// TODO Auto-generated method stub
			this.view.switchView(new HomeView(this.view.Engine));
		}
	}
	
	class OnBtnStartClick implements IAGEventHandler{
		private AGView view;
		public OnBtnStartClick(AGView view){
			this.view = view;
		}
		
		@Override
		public void onHandle(AGControl control) {
			// TODO Auto-generated method stub
			ResultView view2 = (ResultView)this.view;
			if(view2._result.gameMode == SudokuMode.Random){
				view2.switchView(new GameViewV2(this.view.Engine, view2._result.gameLevel));
			}else{
				view2.switchView(new StageGameView(this.view.Engine, view2._result.gameLevel));
			}
		}
	}

}
